package com.hzncc.scaffold.controller.Front.Alliance;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.config.MinioConfig;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.AllianceDetailBo;
import com.hzncc.scaffold.entity.Bo.AllianceListBo;
import com.hzncc.scaffold.entity.Vo.AllianceDetailVo;
import com.hzncc.scaffold.entity.Vo.AllianceInfoVo;
import com.hzncc.scaffold.entity.Vo.AllianceListVo;
import com.hzncc.scaffold.entity.Vo.Institute;
import com.hzncc.scaffold.mapper.AllianceMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-15 11:00
 * @Description: null
 */
@RequestMapping("/alliance")
@Api(tags = "前台接口-联盟单位")
@RestController
public class AllianceController {

    @Autowired
    AllianceMapper allianceMapper;

    @Autowired
    MinioConfig minioConfig;


    @Value("${project.imgFileSuffix}")
    public String imgFileSuffix;

    @GetMapping("/info")
    @ApiOperation("联盟介绍")
    public ResultT info(){
        AllianceInfoVo allianceInfoVo = allianceMapper.allianceInfo();

        return ResultT.success(allianceInfoVo);
    }


    @GetMapping("/list")
    @ApiOperation("联盟单位列表")
    public ResultT list(){
        List<AllianceListBo> allianceListBos = allianceMapper.list();
        ArrayList<AllianceListVo> allianceListVos = new ArrayList<>();

        for (AllianceListBo allianceListBo : allianceListBos) {
            AllianceListVo allianceListVo = BeanUtil.toBean(allianceListBo, AllianceListVo.class);

            Institute institute = new Institute();
            institute.setId(allianceListBo.getInstituteId());
            institute.setName(allianceListBo.getInstituteName());
            institute.setRole(allianceListBo.getInstituteRole());

            if(StrUtil.isNotEmpty(allianceListBo.getInstituteLogo())){
                institute.setLogo(imgFileSuffix + allianceListBo.getInstituteLogo());
            }

            allianceListVo.setInstitute(institute);

            allianceListVos.add(allianceListVo);
        }

//        PageInfo<AllianceListVo> info = new PageInfo<>();
//        info.setList(allianceListVos);
//        info.setTotal(new PageInfo<>(allianceListBos).getTotal());
        HashMap<String,Object> map = new HashMap<>();
        map.put("rows",allianceListVos);
        map.put("count",new PageInfo<>(allianceListBos).getTotal());

        return ResultT.success(map);
    }

    @GetMapping("/detail")
    @ApiOperation("联盟单位详情")
    public ResultT detail(Integer allianceId){
        QueryWrapper<AllianceDetailBo> qw = new QueryWrapper<>();
        qw.eq("applianceapply.id",allianceId);

        AllianceDetailBo allianceDetailBo = allianceMapper.detail(qw);

        AllianceDetailVo allianceDetailVo = BeanUtil.toBean(allianceDetailBo, AllianceDetailVo.class);
        Institute institute = new Institute();
        institute.setId(allianceDetailBo.getInstituteId());
        institute.setName(allianceDetailBo.getInstituteName());
        institute.setLogo(allianceDetailBo.getInstituteLogo());
        institute.setRole(allianceDetailBo.getInstituteRole());

        allianceDetailVo.setInstitute(institute);


        return ResultT.success(allianceDetailVo);
    }
}
